package com.gopay.bis.manager.bank.ebank;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class CmbcB2cManager implements BankManagerRemoteService {
	
	@Resource(name="cmbcB2cBankServiceClient")
	private BankService cmbcB2cBankServiceClient;
	
	@Resource(name="cmbcB2cBankServiceClientHna")
	private BankService cmbcB2cBankServiceClientHna;
	
	@Resource(name = "cmbcConfig")
	private Map<String, String> cmbcConfig;
	

	@Override
	public Object sign(BaseMessage msg) throws GopayException {
		CmbcMessage cmbcMessage = (CmbcMessage) msg;
		buildOrderId(msg);
		String signMessage = "";
		
		cmbcMessage = buildMessage(cmbcMessage);
		cmbcMessage.setCorpName(cmbcConfig.get("CMBC.B2C.MERCHANTNAME"));
		BankLogUtils.printB2CManagerSignSendLog(cmbcMessage);
		if(cmbcMessage.isHna()){
			signMessage = (String) cmbcB2cBankServiceClientHna.sign(cmbcMessage);	
		}else{
			signMessage = (String) cmbcB2cBankServiceClient.sign(cmbcMessage);
		}
		try {
			cmbcMessage.setSignMessage(new String(signMessage.getBytes("UTF-8"),"GBK"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		BankLogUtils.printB2CManagerSignReturnLog(cmbcMessage);
		return cmbcMessage;
	}

	/**
	 * 构建B2C报文
	 * 
	 * @param message
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	private CmbcMessage buildMessage(CmbcMessage message) {
		//message.setCorpID(message.getMerID());
		//message.setCorpID(message.getMerID2nd());
		if (!message.isSupportCredit()) {
			// 充值
			message.setCorpID((String) cmbcConfig.get("CMBC.B2C.RECHARGE.MERCHANTID"));
		} else {
			// 支付
			message.setCorpID((String) cmbcConfig.get("CMBC.B2C.MERCHANTID"));
		}
		message.setCorpName(cmbcConfig.get("CMBC.B2C.MERCHANTNAME"));
		message.setCorpRetType((String) cmbcConfig.get("CMBC.B2C.CORP.RET.TYPE"));
		message.setRetUrl(message.getPayRuturnUrl());
		message.setCurCode((String) cmbcConfig.get("TO.BANK.CURCODE.RMB"));
		message.setBankUrl(message.getPayUrl());
		StringBuffer sb = new StringBuffer("");
		sb.append(message.getBankOrderId()).append("|");
		sb.append(message.getAmount().toString()).append("|");
		sb.append(message.getCurCode()).append("|");
		sb.append(message.getOrderDate().substring(0, 8)).append("|");
		sb.append(message.getOrderDate().substring(8, 14)).append("|");
		sb.append(message.getCorpID()).append("|");
		sb.append(message.getCorpName()).append("|");
		sb.append(message.getBillremark1()).append("|");
		sb.append(message.getBillremark2()).append("|");
		sb.append(message.getCorpRetType()).append("|");
		sb.append(message.getRetUrl()).append("|");
		sb.append(message.getLangMAC());
		message.setPlainMessage(sb.toString());
		return message;
	}

	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		CmbcMessage cmbcMessage = (CmbcMessage) msg;
		BankLogUtils.printB2CManagerVerifySendLog(msg);
		boolean flag = false;
		String isVerify;
		if(cmbcMessage.isHna()){
			isVerify = (String) cmbcB2cBankServiceClientHna.verify(cmbcMessage);	
		}else{
			isVerify = (String) cmbcB2cBankServiceClient.verify(cmbcMessage);
		}
		HashMap map = new HashMap();
		if(isVerify!=null && !"".equals(isVerify)){
			cmbcMessage.setPlainMessage(isVerify);
			String[] str =getMessage(isVerify);
			cmbcMessage.setOrderId(str[0]);
			cmbcMessage.setBankOrderId(str[0]);
			cmbcMessage.setAmount(new BigDecimal(str[2]));
			cmbcMessage.setOrderStatus(str[5]);
			flag = true;
			map.put("flag",flag);
			map.put("cmbcMessage",cmbcMessage);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(msg, isVerify);
		return map;
	}

	@Override
	public Object query(Object obj) throws GopayException {
		CmbcMessage cmbcMessage = null;
		CmbcMessage resMessage = null;
		cmbcMessage = buildCmbcBillMessage(obj);
		BankLogUtils.printB2CManagerQuerySendLog(cmbcMessage, "");
		if(cmbcMessage.isHna()){
			resMessage = (CmbcMessage) this.cmbcB2cBankServiceClientHna.query(cmbcMessage);
		}else{
			resMessage = (CmbcMessage) this.cmbcB2cBankServiceClient.query(cmbcMessage);
		}
		BankLogUtils.printB2CManagerQueryReturnLog(cmbcMessage);
		return resMessage;
	}

	private CmbcMessage buildCmbcBillMessage(Object obj) {
		CmbcMessage cmbcMessage = (CmbcMessage) obj;
		//cmbcMessage.setCorpID(cmbcMessage.getMerID());
		//cmbcMessage.setCorpID(cmbcMessage.getMerID2nd());
		if (!cmbcMessage.isSupportCredit()) {
			// 充值
			cmbcMessage.setCorpID((String) cmbcConfig.get("CMBC.B2C.RECHARGE.MERCHANTID"));
		} else {
			// 支付
			cmbcMessage.setCorpID((String) cmbcConfig.get("CMBC.B2C.MERCHANTID"));
		}
		StringBuffer sf = new StringBuffer("");
		sf.append(cmbcMessage.getCorpID()).append("|");
		sf.append(cmbcMessage.getOrderId());
		cmbcMessage.setPlainMessage(sf.toString());
		return cmbcMessage;
	}
	public String [] getMessage(String msg){
		String [] str =msg.split("\\|");
		return str;
	}
	public void buildOrderId(BaseMessage base){
		String bankOrderId = base.getOrderId().substring(0,8);
		bankOrderId = bankOrderId+base.getOrderId().substring(9);
		bankOrderId = base.getMerID2nd()+bankOrderId;
        base.setBankOrderId(base.getOrderId());
	 }

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
}
